from fastapi import APIRouter, Query
from app.services.data_service import DataService

router = APIRouter()
data_service = DataService()


@router.get("/test")
async def test_stocks():
    """股票模块测试接口"""
    return {"message": "Stocks module is working"}


@router.get("/list")
async def get_stock_list():
    """获取股票列表"""
    stocks = data_service.get_stock_list()
    return {"stocks": stocks}


@router.get("/search")
async def search_stocks(q: str = Query("", description="搜索关键词")):
    """搜索股票"""
    stocks = data_service.search_stocks(q)
    return {"stocks": stocks}


@router.get("/{stock_code}/daily")
async def get_stock_daily_data(
    stock_code: str,
    start_date: str = Query(None, description="开始日期 YYYYMMDD"),
    end_date: str = Query(None, description="结束日期 YYYYMMDD")
):
    """获取股票日K数据"""
    daily_data = data_service.get_stock_daily_data(stock_code, start_date, end_date)
    return {
        "stock_code": stock_code,
        "data": daily_data
    }


@router.get("/{stock_code}/realtime")
async def get_realtime_price(stock_code: str):
    """获取实时股价"""
    price_data = data_service.get_realtime_price(stock_code)
    if price_data:
        return price_data
    else:
        return {"error": "无法获取股票数据"}